package com.luhuiguo.cms.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.luhuiguo.cms.client.presenter.AppPresenter;

/**
 * 应用视图。
 * 
 * @author LuHuiguo
 * @version $Id: AppView.java 96 2010-03-21 07:28:41Z luhuiguo@gmail.com $
 */
@Singleton
public class AppView extends Composite implements AppPresenter.Display {

    private static AppViewUiBinder uiBinder = GWT.create(AppViewUiBinder.class);

    interface AppViewUiBinder extends UiBinder<DockLayoutPanel, AppView> {
    }

    @UiField(provided = true)
    HeaderView headerView;

    @UiField(provided = true)
    FooterView footerView;

    @UiField(provided = true)
    NavigationView navigationView;

    @UiField(provided = true)
    MainView mainView;

    @UiField
    HTMLPanel flashContainer;

    @UiField
    Label flashLabel;

    @Inject
    public AppView(HeaderView headerView, FooterView footerView, NavigationView navigationView, MainView mainView) {
        this.headerView = headerView;
        this.footerView = footerView;
        this.navigationView = navigationView;
        this.mainView = mainView;

        DockLayoutPanel outer = uiBinder.createAndBindUi(this);
        initWidget(outer);

        Element topElem = outer.getWidgetContainerElement(this.headerView);
        topElem.getStyle().setZIndex(2);
        topElem.getStyle().setOverflow(Overflow.VISIBLE);
        GWT.log("AppView created");
    }

    @Override
    public void addWidget(Widget widget) {

    }

    @Override
    public void removeWidget(Widget widget) {

    }

    @Override
    public void showWidget(Widget widget) {

    }

    @Override
    public void showMessage(String message, int millisecs) {
        flashLabel.setText(message);
        flashContainer.setVisible(true);
        if (millisecs > 0)
            hideMessage.schedule(millisecs);
    }

    private final Timer hideMessage = new Timer() {
        public void run() {
            flashContainer.setVisible(false);
            flashLabel.setText("");
        }
    };

    @Override
    public Widget asWidget() {
        return this;
    }

}
